/*
 * ScubaLogbookView.java
 */

package scubalogbook;

import java.awt.Image;
import java.awt.Toolkit;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import scubalogbook.data.ScubaLogbookFilesFilter;
import scubalogbook.data.Sqlite;
import scubalogbook.data.imports.jdivelog.JDiveLogImport;
import scubalogbook.gui.panels.BasesPanel;
import scubalogbook.gui.panels.BuddyPanel;
import scubalogbook.gui.panels.EquipmentPanel;
import scubalogbook.gui.panels.LogBookPanel;
import scubalogbook.gui.panels.PlanningPanel;
import scubalogbook.gui.panels.SettingsPanel;

/**
 * The application's main frame.
 */
public class ScubaLogbookView extends FrameView {   
    public enum PanelName {
        Logbook,
        Equipment,
        Buddy,
        Bases,
        Planning,
        Settings
    }
        
    public static scubalogbook.data.Sqlite sqlite = null;
    public static final File CONFIG_FILE = new File(System.getProperty("user.home"), ".scubaLogbook"); 
    public static final String PROPERTY_LAST_OPENED_DB_FILE = "property_last_opened_db_file"; 
    
    public ScubaLogbookView(SingleFrameApplication app) {
        
        super(app);
        initComponents();
        
        CreateLogger();
        
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });  
            
        try
        {   
            // Loadconfig
            LoadConfigFile();
            
            // Load image into left nav panel
            Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("resources/leftNavBackground.png"));
            leftNavImagePanel.setImage(image);         

            if (sqlite != null)
            {
                // Display Logbook panel as default
                DisplayPanel(PanelName.Logbook);                               
            }
            else
            {                
                FirstRunDialog firstRunDialog = new FirstRunDialog(this, true);
                firstRunDialog.setVisible(true);
                if (sqlite != null)
                {
                    DisplayPanel(PanelName.Logbook);
                }
            }
        }
        catch (Exception exc)
        {    
            Logger logger = Logger.getLogger("scubaLogbook");  
            logger.log(Level.SEVERE, "Application error", exc); 
            
            JOptionPane.showMessageDialog(this.getFrame(),
                exc.getMessage(),
                "Application error",
                JOptionPane.ERROR_MESSAGE);
        }
    }
    
     private void CreateLogger() {
        try {
            // Create an appending file handler
            boolean append = true;
            FileHandler handler = new FileHandler(System.getProperty("user.home") + "scubaLogbook.log", append);

            // Add to the desired logger
            Logger logger = Logger.getLogger("scubaLogbook");
            logger.addHandler(handler);
        } catch (IOException e) {
        }
    }

    public void DisplayNewProfileWindow() {
        try
        {            
            JFileChooser fc = new JFileChooser();
            fc.addChoosableFileFilter(new scubalogbook.data.ScubaLogbookFilesFilter());
            fc.setAcceptAllFileFilterUsed(false);
            int returnVal = fc.showSaveDialog(mainPanel);
            if (returnVal == JFileChooser.APPROVE_OPTION) {            
                File file = fc.getSelectedFile();
                String extension = Common.getExtension(file);
                String dbFilePath = file.getPath();
                if ((extension == null) || (!extension.equals(ScubaLogbookFilesFilter.ScubaLogbookExtension)))
                {
                    dbFilePath = dbFilePath + "." + ScubaLogbookFilesFilter.ScubaLogbookExtension;
                }
                sqlite = new scubalogbook.data.Sqlite(dbFilePath);
                sqlite.CreateNewProfile();
            }
        }
        catch (Exception exc)
        {
            Logger logger = Logger.getLogger("scubaLogbook");  
            logger.log(Level.SEVERE, "Cannot create new profile", exc);

            JOptionPane.showMessageDialog(this.getFrame(),
                    exc.getMessage(),
                    "Cannot create new profile",
                    JOptionPane.ERROR_MESSAGE);
        }        
    }

    public void DisplayOpenProfileWindow() {
        try
        {
            JFileChooser fc = new JFileChooser();
            fc.addChoosableFileFilter(new scubalogbook.data.ScubaLogbookFilesFilter());
            fc.setAcceptAllFileFilterUsed(false);
            int returnVal = fc.showOpenDialog(mainPanel);
            if (returnVal == JFileChooser.APPROVE_OPTION) {            
                String dbFilePath = fc.getSelectedFile().getPath();            
                sqlite = new scubalogbook.data.Sqlite(dbFilePath);            
            }  
        }
        catch (Exception exc)
        {
            Logger logger = Logger.getLogger("scubaLogbook");  
            logger.log(Level.SEVERE, "Cannot open new profile", exc);

            JOptionPane.showMessageDialog(this.getFrame(),
                    exc.getMessage(),
                    "Cannot open new profile",
                    JOptionPane.ERROR_MESSAGE);
        }         
    }

    private void LoadConfigFile() throws Exception {
        if (CONFIG_FILE.exists() && CONFIG_FILE.canRead()) {                        
            Properties properties = new Properties();
            try {
                properties.load(new FileInputStream(CONFIG_FILE));                
                String lastOpenedDbFile = properties.getProperty(PROPERTY_LAST_OPENED_DB_FILE);
                if (lastOpenedDbFile != null) {
                    sqlite = new Sqlite(lastOpenedDbFile);                    
                }
            } catch (FileNotFoundException e) {
                Logger logger = Logger.getLogger("scubaLogbook");  
                logger.log(Level.SEVERE, "failed to load properties", e);                
            } catch (IOException e) {
                Logger logger = Logger.getLogger("scubaLogbook");  
                logger.log(Level.SEVERE, "failed to load properties", e);
            }
        }   

    }
        
    public void DisplayPanel(PanelName activePane)
    {    
        try
        {
            JPanel newPanel = null;

            if (activePane.equals(PanelName.Logbook)){
                newPanel = new LogBookPanel();
                newPanel.setName("logBookPanel");        
            }
            else if (activePane.equals(PanelName.Equipment)){
                newPanel = new EquipmentPanel();
                newPanel.setName("equipmentPanel");       
            }
            else if (activePane.equals(PanelName.Buddy)){
                newPanel = new BuddyPanel();
                newPanel.setName("buddyPanel");
            }
            else if (activePane.equals(PanelName.Bases)){
                newPanel = new BasesPanel();
                newPanel.setName("basesPanel");
            }
            else if (activePane.equals(PanelName.Planning)){
                newPanel = new PlanningPanel();
                newPanel.setName("planningPanel");       
            }
            else if (activePane.equals(PanelName.Settings)){
                newPanel = new SettingsPanel();
                newPanel.setName("settingsPanel"); 
            }

            if (!newPanel.equals(null))
            {
                contentPanel.removeAll();
                contentPanel.repaint();

                javax.swing.GroupLayout contentPanelLayout = new javax.swing.GroupLayout(contentPanel);
                contentPanel.setLayout(contentPanelLayout);
                contentPanelLayout.setHorizontalGroup(
                    contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(newPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 737, Short.MAX_VALUE)
                );
                contentPanelLayout.setVerticalGroup(
                    contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(newPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
                );
            }
        }
         catch (Exception exc)
        {
            Logger logger = Logger.getLogger("scubaLogbook");  
            logger.log(Level.SEVERE, "Cannot create new profile", exc); 
            
            JOptionPane.showMessageDialog(this.getFrame(),
                    exc.getMessage(),
                    "Cannot create new profile",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = ScubaLogbookApp.getApplication().getMainFrame();
            aboutBox = new ScubaLogbookAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        ScubaLogbookApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        mainPanel2 = new javax.swing.JPanel();
        contentPanel = new javax.swing.JPanel();
        leftNavImagePanel = new scubalogbook.gui.components.ImagePanel();
        logbookLabel = new javax.swing.JLabel();
        equipmentLabel = new javax.swing.JLabel();
        basesLabel = new javax.swing.JLabel();
        planningLabel = new javax.swing.JLabel();
        buddyLabel = new javax.swing.JLabel();
        settingsLabel = new javax.swing.JLabel();
        mainToolBar = new javax.swing.JToolBar();
        newProfileButton = new javax.swing.JButton();
        openProfileButton = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        newProfileMenuItem = new javax.swing.JMenuItem();
        openProfileMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        importJDiveLogMenuItem = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        mainPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        mainPanel2.setName("mainPanel2"); // NOI18N

        contentPanel.setName("contentPanel"); // NOI18N

        javax.swing.GroupLayout contentPanelLayout = new javax.swing.GroupLayout(contentPanel);
        contentPanel.setLayout(contentPanelLayout);
        contentPanelLayout.setHorizontalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 658, Short.MAX_VALUE)
        );
        contentPanelLayout.setVerticalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 488, Short.MAX_VALUE)
        );

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(scubalogbook.ScubaLogbookApp.class).getContext().getResourceMap(ScubaLogbookView.class);
        leftNavImagePanel.setBackground(resourceMap.getColor("leftNavImagePanel.background")); // NOI18N
        leftNavImagePanel.setName("leftNavImagePanel"); // NOI18N

        logbookLabel.setForeground(resourceMap.getColor("logbookLabel.foreground")); // NOI18N
        logbookLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        logbookLabel.setIcon(resourceMap.getIcon("logbookLabel.icon")); // NOI18N
        logbookLabel.setText(resourceMap.getString("logbookLabel.text")); // NOI18N
        logbookLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        logbookLabel.setName("logbookLabel"); // NOI18N
        logbookLabel.setPreferredSize(new java.awt.Dimension(100, 60));
        logbookLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        logbookLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                logbookLabelMouseClicked(evt);
            }
        });

        equipmentLabel.setForeground(resourceMap.getColor("equipmentLabel.foreground")); // NOI18N
        equipmentLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        equipmentLabel.setIcon(resourceMap.getIcon("equipmentLabel.icon")); // NOI18N
        equipmentLabel.setText(resourceMap.getString("equipmentLabel.text")); // NOI18N
        equipmentLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        equipmentLabel.setName("equipmentLabel"); // NOI18N
        equipmentLabel.setPreferredSize(new java.awt.Dimension(100, 60));
        equipmentLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        equipmentLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                equipmentLabelMouseClicked(evt);
            }
        });

        basesLabel.setForeground(resourceMap.getColor("basesLabel.foreground")); // NOI18N
        basesLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        basesLabel.setIcon(resourceMap.getIcon("basesLabel.icon")); // NOI18N
        basesLabel.setText(resourceMap.getString("basesLabel.text")); // NOI18N
        basesLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        basesLabel.setName("basesLabel"); // NOI18N
        basesLabel.setPreferredSize(new java.awt.Dimension(100, 60));
        basesLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        basesLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                basesLabelMouseClicked(evt);
            }
        });

        planningLabel.setForeground(resourceMap.getColor("planningLabel.foreground")); // NOI18N
        planningLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        planningLabel.setIcon(resourceMap.getIcon("planningLabel.icon")); // NOI18N
        planningLabel.setText(resourceMap.getString("planningLabel.text")); // NOI18N
        planningLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        planningLabel.setName("planningLabel"); // NOI18N
        planningLabel.setPreferredSize(new java.awt.Dimension(100, 60));
        planningLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        planningLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                planningLabelMouseClicked(evt);
            }
        });

        buddyLabel.setForeground(resourceMap.getColor("buddyLabel.foreground")); // NOI18N
        buddyLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        buddyLabel.setIcon(resourceMap.getIcon("buddyLabel.icon")); // NOI18N
        buddyLabel.setText(resourceMap.getString("buddyLabel.text")); // NOI18N
        buddyLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buddyLabel.setName("buddyLabel"); // NOI18N
        buddyLabel.setPreferredSize(new java.awt.Dimension(100, 60));
        buddyLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buddyLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buddyLabelMouseClicked(evt);
            }
        });

        settingsLabel.setForeground(resourceMap.getColor("settingsLabel.foreground")); // NOI18N
        settingsLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        settingsLabel.setIcon(resourceMap.getIcon("settingsLabel.icon")); // NOI18N
        settingsLabel.setText(resourceMap.getString("settingsLabel.text")); // NOI18N
        settingsLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        settingsLabel.setName("settingsLabel"); // NOI18N
        settingsLabel.setPreferredSize(new java.awt.Dimension(100, 60));
        settingsLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        settingsLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                settingsLabelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout leftNavImagePanelLayout = new javax.swing.GroupLayout(leftNavImagePanel);
        leftNavImagePanel.setLayout(leftNavImagePanelLayout);
        leftNavImagePanelLayout.setHorizontalGroup(
            leftNavImagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftNavImagePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(leftNavImagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(logbookLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(equipmentLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(planningLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(basesLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buddyLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(settingsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        leftNavImagePanelLayout.setVerticalGroup(
            leftNavImagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftNavImagePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logbookLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(equipmentLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(basesLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(planningLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buddyLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                .addComponent(settingsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout mainPanel2Layout = new javax.swing.GroupLayout(mainPanel2);
        mainPanel2.setLayout(mainPanel2Layout);
        mainPanel2Layout.setHorizontalGroup(
            mainPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanel2Layout.createSequentialGroup()
                .addComponent(leftNavImagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(contentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        mainPanel2Layout.setVerticalGroup(
            mainPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(leftNavImagePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(contentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        mainToolBar.setRollover(true);
        mainToolBar.setName("mainToolBar"); // NOI18N

        newProfileButton.setIcon(resourceMap.getIcon("newProfileButton.icon")); // NOI18N
        newProfileButton.setText(resourceMap.getString("newProfileButton.text")); // NOI18N
        newProfileButton.setToolTipText(resourceMap.getString("newProfileButton.toolTipText")); // NOI18N
        newProfileButton.setFocusable(false);
        newProfileButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        newProfileButton.setName("newProfileButton"); // NOI18N
        newProfileButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        newProfileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newProfileButtonActionPerformed(evt);
            }
        });
        mainToolBar.add(newProfileButton);

        openProfileButton.setIcon(resourceMap.getIcon("openProfileButton.icon")); // NOI18N
        openProfileButton.setText(resourceMap.getString("openProfileButton.text")); // NOI18N
        openProfileButton.setToolTipText(resourceMap.getString("openProfileButton.toolTipText")); // NOI18N
        openProfileButton.setFocusable(false);
        openProfileButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        openProfileButton.setName("openProfileButton"); // NOI18N
        openProfileButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        openProfileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openProfileButtonActionPerformed(evt);
            }
        });
        mainToolBar.add(openProfileButton);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 792, Short.MAX_VALUE)
            .addComponent(mainPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(mainToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mainPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        newProfileMenuItem.setIcon(resourceMap.getIcon("newProfileMenuItem.icon")); // NOI18N
        newProfileMenuItem.setMnemonic('N');
        newProfileMenuItem.setText(resourceMap.getString("newProfileMenuItem.text")); // NOI18N
        newProfileMenuItem.setName("newProfileMenuItem"); // NOI18N
        newProfileMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newProfileMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(newProfileMenuItem);

        openProfileMenuItem.setIcon(resourceMap.getIcon("openProfileMenuItem.icon")); // NOI18N
        openProfileMenuItem.setMnemonic('O');
        openProfileMenuItem.setText(resourceMap.getString("openProfileMenuItem.text")); // NOI18N
        openProfileMenuItem.setName("openProfileMenuItem"); // NOI18N
        openProfileMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openProfileMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(openProfileMenuItem);

        jSeparator1.setName("jSeparator1"); // NOI18N
        fileMenu.add(jSeparator1);

        importJDiveLogMenuItem.setText(resourceMap.getString("importJDiveLogMenuItem.text")); // NOI18N
        importJDiveLogMenuItem.setName("importJDiveLogMenuItem"); // NOI18N
        importJDiveLogMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importJDiveLogMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(importJDiveLogMenuItem);

        jSeparator2.setName("jSeparator2"); // NOI18N
        fileMenu.add(jSeparator2);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(scubalogbook.ScubaLogbookApp.class).getContext().getActionMap(ScubaLogbookView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 792, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 608, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

private void newProfileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newProfileButtonActionPerformed
    DisplayNewProfileWindow();         
}//GEN-LAST:event_newProfileButtonActionPerformed

private void openProfileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openProfileButtonActionPerformed
    try
    {
       DisplayOpenProfileWindow();     
    }
    catch (Exception exc)
    {
        Logger logger = Logger.getLogger("scubaLogbook");  
        logger.log(Level.SEVERE, "Cannot open new profile", exc);
        
        JOptionPane.showMessageDialog(this.getFrame(),
                exc.getMessage(),
                "Cannot open new profile",
                JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_openProfileButtonActionPerformed

private void logbookLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_logbookLabelMouseClicked
    DisplayPanel(PanelName.Logbook);
}//GEN-LAST:event_logbookLabelMouseClicked

private void equipmentLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_equipmentLabelMouseClicked
    DisplayPanel(PanelName.Equipment);
}//GEN-LAST:event_equipmentLabelMouseClicked

private void basesLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_basesLabelMouseClicked
    DisplayPanel(PanelName.Bases);
}//GEN-LAST:event_basesLabelMouseClicked

private void planningLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_planningLabelMouseClicked
    DisplayPanel(PanelName.Planning);
}//GEN-LAST:event_planningLabelMouseClicked

private void buddyLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buddyLabelMouseClicked
    DisplayPanel(PanelName.Buddy);
}//GEN-LAST:event_buddyLabelMouseClicked

private void settingsLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_settingsLabelMouseClicked
    DisplayPanel(PanelName.Settings);
}//GEN-LAST:event_settingsLabelMouseClicked

private void newProfileMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newProfileMenuItemActionPerformed
    DisplayNewProfileWindow();
}//GEN-LAST:event_newProfileMenuItemActionPerformed

private void openProfileMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openProfileMenuItemActionPerformed
    DisplayOpenProfileWindow();
}//GEN-LAST:event_openProfileMenuItemActionPerformed

private void importJDiveLogMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importJDiveLogMenuItemActionPerformed
    try
        {
            JFileChooser fc = new JFileChooser();
            fc.addChoosableFileFilter(new scubalogbook.data.imports.jdivelog.JDiveLogFilesFilter());
            fc.setAcceptAllFileFilterUsed(false);
            int returnVal = fc.showOpenDialog(mainPanel);
            if (returnVal == JFileChooser.APPROVE_OPTION) {            
                File file = fc.getSelectedFile();           
                
                JDiveLogImport jDiveLogImport = new JDiveLogImport();
                jDiveLogImport.ConvertJDiveLogIntoScubaLogbook(file);               
            }  
        }
        catch (Exception exc)
        {
            Logger logger = Logger.getLogger("scubaLogbook");  
            logger.log(Level.SEVERE, "Cannot open new profile", exc);

            JOptionPane.showMessageDialog(this.getFrame(),
                    exc.getMessage(),
                    "Cannot open new profile",
                    JOptionPane.ERROR_MESSAGE);
        }      
}//GEN-LAST:event_importJDiveLogMenuItemActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel basesLabel;
    private javax.swing.JLabel buddyLabel;
    private javax.swing.JPanel contentPanel;
    private javax.swing.JLabel equipmentLabel;
    private javax.swing.JMenuItem importJDiveLogMenuItem;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private scubalogbook.gui.components.ImagePanel leftNavImagePanel;
    private javax.swing.JLabel logbookLabel;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JPanel mainPanel2;
    private javax.swing.JToolBar mainToolBar;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JButton newProfileButton;
    private javax.swing.JMenuItem newProfileMenuItem;
    private javax.swing.JButton openProfileButton;
    private javax.swing.JMenuItem openProfileMenuItem;
    private javax.swing.JLabel planningLabel;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel settingsLabel;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}
